package com.distribute.application.api.rest.member;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.distribute.application.api.BaseApiController;
import com.distribute.application.api.utils.MapHelper;
import com.distribute.framework.core.lang.Protocol;
import com.distribute.framework.core.lang.title;
import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.model.BrandApplication;
import com.distribute.framework.model.Headline;
import com.distribute.framework.model.HelpCategory;
import com.distribute.framework.model.Member;
import com.distribute.framework.model.Merchant;
import com.distribute.framework.model.OrderDetails;
import com.distribute.framework.model.Region;
import com.distribute.framework.model.TbPartners;
@RestController(value="api.BanrdController")
public class BanrdController extends BaseApiController{
	/**
	 * 会员获取品牌名称
	 * @return
	 */
	@RequestMapping("/api/banrd/getBanrdName.do")
	public @title("会员获取品牌名称") Protocol getBanrdName(
			@title("品牌ID") Long banrdId
			){
		Protocol protocol = Protocol.create(true, null);
		Merchant merchant=Merchant.where.id(banrdId).get();
		protocol.set("banrdName", merchant.getBrandName());
		return protocol;
	}
	/**
	 * 会员获取品牌名称
	 * @return
	 */
	@RequestMapping("/api/banrd/banrdList.do")
	public @title("会员获取品牌名称") Protocol banrdList(
			){
		Protocol protocol = Protocol.create(true, null);
		List<Merchant> merchants=Merchant.where.openState((short)2).status((short)1).list(-1);
		protocol.setList(merchants,   new Protocol.BeanFormat<Merchant>(){
			@Override
			public Map<String, Object> format(Merchant t) {
				Map<String, Object> map=new HashMap<String, Object>();
				map.put("id", t.getId());
				map.put("name", t.getBrandName());
				map.put("img", t.getBanner());
				return map;
			}
		});
		return protocol;
	}
	/**
	 * 判断会员是否申请过品牌
	 * @return
	 */
	@RequestMapping("/api/banrd/isApplyBanrd.do")
	public @title("判断会员是否申请过品牌") Protocol isApplyBanrd(
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		BrandApplication brandApplication=BrandApplication.where.member(member.getId()).get();
		if (brandApplication!=null) {
			protocol.set("obj", brandApplication.toJSON());
			return protocol;
		}else {
			protocol.set("obj", null);
			return protocol;
		}
	}
	/**
	 * 会员提交品牌申请
	 * @return
	 */
	@RequestMapping("/api/banrd/saveApplyBanrd.do")
	public @title("会员提交品牌申请") Protocol saveApplyBanrd(
			@title("品牌名称") String name,
			@title("手机号") String mobile,
			@title("联系方式") String lianxi,
			@title("公司名称") String banrdName,
			@title("备注") String remark
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		BrandApplication brandApplication=new BrandApplication(
				member.getId()//申请人
			 	,name//品牌名称
			 	,banrdName//公司名称
			 	,lianxi //联系人
			 	,mobile//联系电话
			 	,remark//备注
			 	,new Date()//修改时间 [非空]
			 	,new Date()//添加时间 [非空]
			 	,(short)0//状态 [非空]
				);
		    brandApplication.save();
			protocol.set("msg", "提交成功");
			return protocol;
	}
	/**
	 * 会员提交修改品牌申请
	 * @return
	 */
	@RequestMapping("/api/banrd/editApplyBanrd.do")
	public @title("会员提交修改品牌申请") Protocol editApplyBanrd(
			@title("编号") Long id,
			@title("品牌名称") String name,
			@title("手机号") String mobile,
			@title("联系方式") String lianxi,
			@title("公司名称") String banrdName,
			@title("备注") String remark
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		BrandApplication brandApplication=BrandApplication.where.id(id).get();
		brandApplication.setBrandName(name);
		brandApplication.setConName(banrdName);
		brandApplication.setContacts(lianxi);
		brandApplication.setMobile(mobile);
		brandApplication.setRemarks(remark);
		brandApplication.setUpdateTime(new Date());
		brandApplication.update();
			protocol.set("msg", "提交成功");
			return protocol;
	}
	/**
	 * 根据品牌的ID取品牌信息
	 * @return
	 */
	@RequestMapping("/api/banrd/getBanrdInfos.do")
	public @title("根据品牌的ID取品牌信息") Protocol getBanrdInfos(
			@title("品牌编号") Long banrdId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Merchant merchant=Merchant.where.id(banrdId).get();
		protocol.set("merchant", merchant.toJSON());
		return protocol;
	}
	/**
	 * 获取最近合伙人加品牌商信息
	 * @return
	 */
	@RequestMapping("/api/banrd/getHHBanrdInfos.do")
	public @title("根据品牌的ID取品牌信息") Protocol getHHBanrdInfos(
			@title("品牌编号") Long banrdId,
			@title("纬度") String latitude,
			@title("经度") String longitude
			,@title("城市") String city
			,@title("省份") String province
			,@title("区县") String country
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		Region region=Region.where.name(province).level((short)1).get();
		 Long cid=null;
		if (region!=null) {
			Region region2=Region.where.parent(region.getId()).name(city).level((short)2).get();
			if (region2!=null) {
				Region region3=Region.where.parent(region2.getId()).name(country).level((short)3).get();
				cid=region3.getId();
			}
		}
		 if (cid!=null) {
			 String aString=latitude+","+longitude;
			 List<TbPartners> tbPartners=TbPartners.where.tbpCounty(cid).tbpPayCard((short)2).list(-1);
			 List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
			 if (tbPartners.size()>0) {
				for (TbPartners tbPartners2 : tbPartners) {
					if(!tbPartners2.getTbpMember().toString().equals(member.getId().toString())){
						Map<String, Object> map=new HashMap<String, Object>();
						String bString=tbPartners2.getTbpLatitude()+","+tbPartners2.getTbpLongitude();
						double lj=MapHelper.GetPointDistance(aString, bString);	
						map.put("name", tbPartners2.getTbpName());
						map.put("mobile", tbPartners2.getTbpMobile());
						map.put("jl", lj);
						list.add(map);
					}
				}
			 protocol.set("slist", list);
			}else {
			protocol.set("slist", new ArrayList<String>());
			}
		}else {
			protocol.set("slist", new ArrayList<String>());
		}
		
		
		
		Merchant merchant=Merchant.where.id(banrdId).get();
		protocol.set("merchant", merchant.toJSON());
		return protocol;
	}
	/**
	 * 获取滚动品牌头条
	 * @return
	 */
	@RequestMapping("/api/banrd/getScrollLists.do")
	public @title("获取滚动品牌头条") Protocol getScrollLists(
			@title("品牌编号") Long banrdId
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.setList(Headline.where.merchant(banrdId).scroll((short)1).list(-1),  new Protocol.BeanFormat<Headline>(){
			@Override
			public Map format(Headline t) {
				Map json = t.toJSON();
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 获取当前品牌帮助问题列表
	 * @return
	 */
	@RequestMapping("/api/banrd/getHelpsLists.do")
	public @title("获取当前品牌帮助问题列表") Protocol getHelpsLists(
			@title("品牌编号") Long banrdId
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		protocol.setList(HelpCategory.where.merchant(banrdId).list(-1),  new Protocol.BeanFormat<HelpCategory>(){
			@Override
			public Map format(HelpCategory t) {
				Map json = t.toJSON();
				return json;
			}
		});
		return protocol;
	}
	/**
	 * 根据问题编号获取问题详情
	 * @return
	 */
	@RequestMapping("/api/banrd/getHelpsDetails.do")
	public @title("根据问题编号获取问题详情") Protocol getHelpsDetails(
			@title("问题编号") Long qustionId
			
			){
		Protocol protocol = Protocol.create(true, null);
		Member member=Member.where.uid(this.getUid()).get();
		if (member==null) {
			return Protocol.create(false, "操作错误","not_found_user");
		}
		HelpCategory helpCategory=HelpCategory.where.id(qustionId).get();
		protocol.set("obj", helpCategory.toJSON());
		return protocol;
	}
}
